/*
 * Copyright (c) 2011 Jean-Marc Perronne
 * Ensisa, 12 rue des Freres Lumieres, F-68093 Mulhouse cedex
 * All rights reserved.
 *
 * The copyright of this software is owned by Werner Randelshofer. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Jean-Marc Perronne. For details see accompanying license terms. 
 */

package fr.uha.ensisa.jmp.ui.mvc;

import java.awt.LayoutManager;

import javax.swing.JPanel;

public abstract class View<M> extends JPanel
{
	private M model;
	private Controller<M> controller;

	public View(M model)
	{
		this.initialize(model);
	}

	public View(M model, LayoutManager layout)
	{
		super(layout);
		this.initialize(model);
	}

	private void initialize(M model)
	{
		this.model = model;
		this.controller = defaultController(model);
		this.controller.view(this);
		this.addMouseListener(this.controller);
		this.addMouseMotionListener(this.controller);
		this.addMouseWheelListener(this.controller);
		this.addKeyListener(this.controller);
		
	}
	
	public void setModel(M model)
	{
		this.model = model;
		this.controller.model(model);
	}
	
	public M model()
	{
		return this.model;
	}
	
	public Controller<M> defaultController(M model)
	{
		return new Controller<M>(model);
	}
	
	final public Controller<M> controller()
	{
		return this.controller;
	}
}


